// import * as ddscode from "dingtalk-jsapi";
let app = getApp();

Page({
  data: {
    user: "",
    // checked: true,
    // isShow: false,
    // defaultChecked: false,
    // nick: 'randi',
    // avatarUrl: "/image/xinxi.jpg",
    // senPiNumber: 0,
    // caoSongNumber: 0,
    // wodeNumber: 0,
    changePassword: false,    //修改密码是否要弹出
    p1: "",
    p2: "",
    new_pwd:"",
  },
  onLoad() {
    var that = this;
    that.getSDKVersion();
    // console.log("app.globaldata.user==>", app.globaldata.user);
    that.setData({
      user: app.globaldata.user.员工姓名,
      gongHao:app.globaldata.user.员工工号,
      gongHaos:app.globaldata.user.部门名称,
    })
  },
  onShow() {
  },

  logOut() {
    dd.confirm({
      title: '温馨提示',
      content: '是否退出当前账号',
      confirmButtonText: '确定退出',
      cancelButtonText: '暂不需要',
      success: (result) => {
        console.log(result);
        if (result.confirm) {
          dd.reLaunch({
            url: '/pages/login/login?stop=1'
          })
        }
      },
    });
  },

  //前往审批
  //  todaishenpi(){
  //   dd.navigateTo({
  //     url: '/pages/shenpi/shenpi',
  //   })
  // },
  getSDKVersion() {
    this.setData({
      content: dd.ExtSDKVersion || dd.SDKVersion,
    })

  },



  //修改密码弹出
  logChangePassword() {
    var that = this;
    that.setData({
      changePassword: true,
    })
  },
  //取消修改密码撤回
  cancelChangePassword() {
    var that = this;
    that.setData({
      changePassword: false,
    })
  },

  //修改密码
  amendChangePassword() {
    var that = this;
    var job_Fid = app.globaldata.user.FID;  //申请人FID
    var new_pwd = that.data.new_pwd;

    var data = {
      "fid": String(job_Fid),
      "pwd_new": new_pwd
    };
    console.log("data==>", data);
    dd.httpRequest({
      url: 'http://openapi.keyboardadmin.com:10003/change_password',
      method: 'POST',
      data: JSON.stringify(data),
      dataType: 'json',
      success: function (res) {
        console.log("调用接口修改密码接口",res);
        if (res.data.ceode == 200) {
        console.log("调用接口res");

          that.showAlertDialog();
          
        } else {
          // dd.alert({ content: res.data.data ,buttonText: '我知道了'});
          dd.showToast({
            type: 'fail',
            content: res.data.data,
            duration: 3000,
          });
        }
       
      },
      fail: function (err) {
        dd.alert({ content: err.data.detail});
      },
      complete: function (res) {
        console.log('ressss==>',res)
        // dd.alert({ content: 'complete' });
        // my.hideLoading();
      }
    });
  },


 // 修改密码成功后要执行的动作
 showAlertDialog() {
  console.log("调用showAlertDialog");
  var that = this;
  // const result = await modal.showAlert('跳转提示', '密码修改成功,前往登录跳转重新登录!!');
  dd.alert({
    title: '密码修改成功,前往登录跳转重新登录!!',
    buttonText: '确定',
    success: () => { 
      that.cancelChangePassword();
        dd.clearStorage({
          key: 'loginMember',
          success: function(){ 
            console.log("清除账号密码缓存")
          }
        });
        dd.reLaunch({
          url: '/pages/login/login'
        })

    },
  })

 
},



  // 获取输入的密码
  password1(e) {
    var that = this;
    that.setData({
      p1: e.detail.value
    });

    console.log("p1修改后===============>", that.data.p1);
  },

  // 获取输入的确定密码
  password2(e) {
    var that = this;
    that.setData({
      p2: e.detail.value
    });
  },


  // 点击确定修改密码执行的操作
  confirm() {
    var that = this;
    var p1 = that.data.p1;
    var p2 = that.data.p2;
    console.log("点击修改密码确定后触发",p1);
    if (p1 == "" || p2 == "") {
      dd.alert({
        title: '未填写密码',
        buttonText: '我知道了',
        success: () => { },
      })
    } else if (p1 != p2) {
      dd.alert({
        title: '输入密码不相同',
        buttonText: '我知道了',
        success: () => { },
      })
    } else {
      that.setData({
        new_pwd: p1
      })
      that.amendChangePassword();

      // console.log(修改成功处理);
    }

  }


});
